package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.bo.CouponBo;
import org.dromara.business.domain.vo.CouponVo;
import org.dromara.business.service.CouponService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券
 *
 * @author zlj
 * @date 2025-04-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/coupon")
public class CouponController extends BaseController {

    private final CouponService couponService;

    /**
     * 查询拼团优惠券列表
     */
//    @SaCheckPermission("business:coupon:list")
    @GetMapping("/list")
    public TableDataInfo<CouponVo> list(CouponBo bo, PageQuery pageQuery) {
        return couponService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出拼团优惠券列表
     */
    @SaCheckPermission("business:coupon:export")
    @Log(title = "拼团优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CouponBo bo, HttpServletResponse response) {
        List<CouponVo> list = couponService.queryList(bo);
        ExcelUtil.exportExcel(list, "拼团优惠券", CouponVo.class, response);
    }

    /**
     * 获取拼团优惠券详细信息
     *
     * @param couponId 主键
     */
    @SaCheckPermission("business:coupon:query")
    @GetMapping("/{couponId}")
    public R<CouponVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long couponId) {
        return R.ok(couponService.queryById(couponId));
    }

    /**
     * 新增拼团优惠券
     */
    @SaCheckPermission("business:coupon:add")
    @Log(title = "拼团优惠券", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CouponBo bo) {
        return toAjax(couponService.insertByBo(bo));
    }

    /**
     * 修改拼团优惠券
     */
    @SaCheckPermission("business:coupon:edit")
    @Log(title = "拼团优惠券", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CouponBo bo) {
        return toAjax(couponService.updateByBo(bo));
    }

    /**
     * 删除拼团优惠券
     *
     * @param couponIds 主键串
     */
    @SaCheckPermission("business:coupon:remove")
    @Log(title = "拼团优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] couponIds) {
        return toAjax(couponService.deleteWithValidByIds(List.of(couponIds), true));
    }

    /**
     * 领取优惠卷
     */
    @PostMapping("/receive")
    public R<Void> receive(@RequestParam Long configId) {
        return toAjax(couponService.receive(configId));
    }
}
